package com.stormrage.edu.serv.edu.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.stormrage.edu.dao.edu.EduChapterDao;
import com.stormrage.edu.dao.edu.EduCourseDao;
import com.stormrage.edu.dao.edu.view.EduCourseChapterDao;
import com.stormrage.edu.dao.edu.view.EduModuleCourseDao;
import com.stormrage.edu.serv.edu.EduCourseServ;
import com.stormrage.edu.serv.edu.EduLecServ;
import com.stormrage.edu.serv.edu.view.EduCourseChapterServ;
import com.stormrage.entity.edu.EduChapter;
import com.stormrage.entity.edu.EduCourse;
import com.stormrage.entity.edu.EduLec;
import com.stormrage.entity.edu.rep.EduCourseRep;
import com.stormrage.entity.edu.req.EduCourseReq;
import com.stormrage.entity.edu.view.EduCourseChapter;
import com.stormrage.entity.exam.rep.ExmCountRep;
import com.stormrage.entity.exam.rep.ExmExamCountRep;
import com.stormrage.entity.learn.req.ExmTermNessReq;
import com.stormrage.entity.rep.ListRep;
import com.stormragetech.core.exception.StormrageTechException;
import com.stormragetech.core.util.FileUtil;

/**
 * @Title: EduCourseServImpl.java
 * @Package com.stormrage.edu.serv.edu.impl
 * @Description: 课程 Service
 * @author www.stormragetech.com
 * @date 2016年4月8日
 * @version V1.0
 */
@Service
public class EduCourseServImpl implements EduCourseServ {

	private static final Logger logger = LoggerFactory.getLogger(EduModuleServImpl.class);

	@Autowired
	private EduChapterDao eduChapterDao;

	@Autowired
	private EduCourseDao eduCourseDao;

	@Autowired
	private EduCourseChapterDao eduCourseChapterDao;

	@Autowired
	private EduModuleCourseDao eduModuleCourseDao;

	@Autowired
	private EduCourseChapterServ eduCourseChapterServ;

	@Autowired
	private EduLecServ eduLecServ;

	@Override
	public Integer create(final EduCourse arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduCourseServ 】- 【 Create 】- 开始运行 ");

		Integer index = eduCourseDao.create(arg0);

		if (arg0.getChap().equals(0)) {
			EduChapter chap = EduChapter.instance(arg0.getCourse() + " - 默认章节");
			Integer id = eduChapterDao.create(chap);
			EduCourseChapter view = EduCourseChapter.instance(index, id);
			eduCourseChapterDao.create(view);
		}

		logger.debug(" 【 Serv 】- 【 EduCourseServ 】- 【 Create 】- 完成运行 ");
		return index;
	}

	@Override
	public void update(final EduCourse arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduCourseServ 】- 【 Update 】- 开始运行 ");

		eduCourseDao.update(arg0);

		logger.debug(" 【 Serv 】- 【 EduCourseServ 】- 【 Update 】- 完成运行 ");
	}

	@Override
	public void delete(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduCourseServ 】- 【 Delete 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		eduCourseDao.delete(map);
		eduCourseChapterDao.deleteByCourseId(map);
		eduModuleCourseDao.deleteByCourseId(map);

		logger.debug(" 【 Serv 】- 【 EduCourseServ 】- 【 Delete 】- 完成运行 ");
	}

	@Override
	public Integer queryCount(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduCourseServ 】- 【 QueryCount 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("query", " WHERE lec_id = :id");
		map.put("id", arg0);

		Integer index = eduCourseDao.queryCount(map);

		logger.debug(" 【 Serv 】- 【 EduCourseServ 】- 【 QueryCount 】- 完成运行 ");
		return index;
	}

	@Override
	public Integer queryCount(final EduCourseReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduCourseServ 】- 【 QueryCount 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("course", arg0.getCourse());

		Integer index = eduCourseDao.queryCount(map);

		logger.debug(" 【 Serv 】- 【 EduCourseServ 】- 【 QueryCount 】- 完成运行 ");
		return index;
	}

	@Override
	public EduCourse queryById(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduCourseServ 】- 【 QueryById 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		EduCourse entity = eduCourseDao.queryById(map);

		logger.debug(" 【 Serv 】- 【 EduCourseServ 】- 【 QueryById 】- 完成运行 ");
		return entity;
	}

	@Override
	public ListRep queryList(final Integer arg0, final EduCourseReq arg1) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduChapterServ 】- 【 QueryList 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		List<EduCourse> array = eduCourseDao.queryList();
		array.removeAll(queryListById(arg0));

		Iterator<EduCourse> it = array.iterator();
		while (it.hasNext()) {
			EduCourse course = it.next();
			if (null != arg1.getCourse() && !arg1.getCourse().equals("")
					&& !course.getCourse().contains(arg1.getCourse()))
				it.remove();
		}

		List<EduCourseRep> list = new ArrayList<EduCourseRep>();
		int seq = arg1.getPage().getStartPage() + 1;

		for (int i = 0; i < array.size(); i++) {
			if (i >= arg1.getPage().getStartPage() && i <= arg1.getPage().getEndPage()) {
				EduCourse course = array.get(i);
				Integer count = eduCourseChapterServ.queryCountById(course.getId());
				EduLec lec = eduLecServ.queryById(course.getLecId());
				EduCourseRep courseRep = EduCourseRep.instance(course, seq, count, lec.getLec());
				list.add(courseRep);
				seq++;
			}
		}

		ListRep rep = ListRep.instance(array.size(), list);

		logger.debug(" 【 Serv 】- 【 EduChapterServ 】- 【 QueryList 】- 完成运行 ");
		return rep;
	}

	@Override
	public ListRep queryList(final EduCourseReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduCourseServ 】- 【 QueryList 】- 开始运行 ");

		Map<String, Object> map = initQuery(arg0);

		Integer count = eduCourseDao.queryCount(map);

		List<EduCourseRep> array = eduCourseDao.queryList(map);

		int seq = 1;

		Iterator<EduCourseRep> it = array.iterator();
		while (it.hasNext()) {
			EduCourseRep module = it.next();
			Integer courCount = eduCourseChapterServ.queryCountById(module.getId());
			module = EduCourseRep.instance(module, seq, courCount);
			seq++;
		}

		ListRep rep = ListRep.instance(count, array);

		logger.debug(" 【 Serv 】- 【 EduCourseServ 】- 【 QueryList 】- 完成运行 ");
		return rep;
	}

	@Override
	public List<EduCourse> queryListById(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduCourseServ 】- 【 QueryList 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		List<EduCourse> array = eduCourseDao.queryListById(map);

		logger.debug(" 【 Serv 】- 【 EduCourseServ 】- 【 QueryList 】- 完成运行 ");
		return array;
	}

	@Override
	public List<EduCourse> queryListByMajorId(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduCourseServ 】- 【 QueryListByMajorId 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		List<EduCourse> array = eduCourseDao.queryListByMajorId(map);

		logger.debug(" 【 Serv 】- 【 EduCourseServ 】- 【 QueryListByMajorId 】- 完成运行 ");
		return array;
	}

	@Override
	public List<EduCourse> queryRepListByTerm(final ExmTermNessReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduCourseServ 】- 【 queryRepListByTerm 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0.getTermId());
		map.put("ness", arg0.getNess());
		
		List<EduCourse> array = eduCourseDao.queryRepListByTerm(map);
		
		logger.debug(" 【 Serv 】- 【 EduCourseServ 】- 【 queryRepListByTerm 】- 完成运行 ");
		return array;
	}

	@Override
	public ExmCountRep queryRepListByTermNess(final ExmTermNessReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduCourseServ 】- 【 queryListByMajorNess 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0.getTermId());
		map.put("ness", arg0.getNess());

		List<ExmExamCountRep> array = eduCourseDao.queryRepListByTermNess(map);
		int seq = 1;
		int sele = 0, mult = 0, corr = 0, total = 0;

		Iterator<ExmExamCountRep> it = array.iterator();
		while (it.hasNext()) {
			ExmExamCountRep entity = it.next();
			entity = ExmExamCountRep.instance(entity, seq);
			sele += entity.getSele();
			mult += entity.getMult();
			corr += entity.getCorr();
			seq++;
		}
		total = sele + mult + corr;

		ExmCountRep rep = ExmCountRep.instance(sele, mult, corr, total, array);

		logger.debug(" 【 Serv 】- 【 EduCourseServ 】- 【 queryListByMajorNess 】- 完成运行 ");
		return rep;
	}

	@Override
	public List<EduCourseRep> queryListByForExam(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduCourseServ 】- 【 QueryListByMajorId 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		List<EduCourse> array = eduCourseDao.queryListByMajorId(map);

		List<EduCourseRep> list = new ArrayList<EduCourseRep>();
		int seq = 1;

		Iterator<EduCourse> it = array.iterator();
		while (it.hasNext()) {
			EduCourse course = it.next();
			Integer courseCount = eduCourseChapterServ.queryCountById(course.getId());
			EduLec lec = eduLecServ.queryById(course.getLecId());
			EduCourseRep courseRep = EduCourseRep.instance(course, seq, courseCount, lec.getLec());
			list.add(courseRep);
			seq++;
		}

		logger.debug(" 【 Serv 】- 【 EduCourseServ 】- 【 QueryListByMajorId 】- 完成运行 ");
		return list;
	}

	@Override
	public List<EduCourseRep> queryRepListById(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduCourseServ 】- 【 QueryList 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		List<EduCourseRep> array = eduCourseDao.queryRepListById(map);

		int seq = 1;

		Iterator<EduCourseRep> it = array.iterator();
		while (it.hasNext()) {
			EduCourseRep courseRep = it.next();
			Integer courseCount = eduCourseChapterServ.queryCountById(courseRep.getId());
			courseRep = EduCourseRep.instance(courseRep, seq, courseCount);
			seq++;
		}

		logger.debug(" 【 Serv 】- 【 EduCourseServ 】- 【 QueryList 】- 完成运行 ");
		return array;
	}

	@Override
	public String upload(final MultipartFile arg0, final String arg1) throws StormrageTechException {

		logger.debug(" 【 Service 】- 【 EduCourseServ 】- 【 Upload 】- 【 STARTED 】");

		String fileName = FileUtil.saveFile(arg0, arg1, "image");

		logger.debug(" 【 Service 】- 【 EduCourseServ 】- 【 Upload 】- 【 COMPLETED 】");
		return fileName;
	}

	private Map<String, Object> initQuery(final EduCourseReq arg0) {

		if (null == arg0)
			return null;

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startPage", arg0.getPage().getStartPage());
		map.put("size", arg0.getPage().getSize());

		StringBuffer query = new StringBuffer("");

		if (null != arg0.getCourse() && arg0.getCourse().length() > 0) {
			query.append("course LIKE :course AND ");
			map.put("course", "%" + arg0.getCourse() + "%");
		}

		String sql = query.toString();
		if (null != sql && sql.length() > 0)
			map.put("query", " AND " + sql.substring(0, sql.length() - 4));
		else
			map.put("query", "");

		return map;
	}

}
